﻿using System;
using System.Configuration;
using System.IO;
using System.ServiceProcess;
using System.Threading;
using System.Timers;

namespace MCConvertZK
{
    public partial class Service1 : ServiceBase
    {
        public string gOldFolder = null;
        private static System.Timers.Timer aTimer;
        string picPath = ConfigurationManager.AppSettings["pathPic"];
        string spanTime = ConfigurationManager.AppSettings["spanTime"];
        int i;
        public Service1()
        {
            InitializeComponent();
            //Init();

        }

        protected override void OnStart(string[] args)
        {
            LogHelper.WriteLog(typeof(Service1), "杰瑞转换电警命名服务启动");

            aTimer = new System.Timers.Timer(6000);


            aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);


            aTimer.Interval = int.Parse(spanTime);
            aTimer.Enabled = true;


        }

        private void OnTimedEvent(object sender, ElapsedEventArgs e)
        {
            i = 0;
            LogHelper.WriteLog(typeof(Service1), "扫描开始");
            convertDJ1(picPath);
            LogHelper.WriteLog(typeof(Service1), "扫描结束共计--" + i.ToString() + "--条数据" );

        }

        private void convertDJ1(string picPath)
        {
            try
            {
                string searchPattern = "*.jpg";

                DirectoryInfo di = new DirectoryInfo(picPath);
                DirectoryInfo[] directories =
                    di.GetDirectories(searchPattern, SearchOption.AllDirectories);

                FileInfo[] files =
                    di.GetFiles(searchPattern, SearchOption.AllDirectories);
                i = files.Length;
                foreach (FileInfo file in files)
                {

                    string fileNameStr = file.FullName;
                    string fileNameOld = System.IO.Path.GetFileNameWithoutExtension(fileNameStr);//文件名  
                    string[] s = fileNameOld.Split(new char[] { '_' });

                    string fileToMove = fileNameStr;
                    string roadCode = s[0];
                    string fileNewFolder = ConfigurationManager.AppSettings["pathPicNew"];

                    string yearStr = s[3].Substring(0, 4);
                    string monthStr = s[3].Substring(4, 2);
                    string dateStr = s[3].Substring(6, 2);
                    string timeStr = s[3].Substring(8, 6);
                    //E:\电子警察\601712001\2016\12\14
                    string fileName = s[0] + "-" + yearStr + "-" + monthStr + "-" + dateStr + "-" + timeStr + "-" + s[3].Substring(14) + "-3-" + s[4].Substring(1) + ".jpg";
                    fileNewFolder = fileNewFolder + @"\" + roadCode + @"\" + yearStr + @"\" + monthStr + @"\" + dateStr + @"\";
                    if (!Directory.Exists(fileNewFolder)) Directory.CreateDirectory(fileNewFolder);

                    string fileNewDestination = fileNewFolder + fileName;

                    //FileInfo file = new FileInfo(fileToMove);
                    if (file.Exists)
                    {
                        //file.CopyTo(fileNewDestination);
                        file.MoveTo(fileNewDestination);
                        File.Delete(fileNameOld);
                        LogHelper.WriteLog(typeof(Service1), fileNewDestination + "转换成功");

                    }
                }
            }
            catch (System.Exception ex)
            {
                LogHelper.WriteLog(typeof(Service1), ex);

            }
            
                


/*


                    string fileNameStr = fileNameObj.ToString();
                string fileNameOld = System.IO.Path.GetFileNameWithoutExtension(fileNameStr);//文件名  
                string[] s = fileNameOld.Split(new char[] { '_' });

                string fileToMove = fileNameStr;
                string roadCode = s[0];
                string fileNewFolder = ConfigurationManager.AppSettings[roadCode];
                if (!Directory.Exists(fileNewFolder)) Directory.CreateDirectory(fileNewFolder);

                string fileName = s[0] + s[1] + s[2].Substring(1) + "-" + s[3].Substring(0, 14) + "-" + s[3].Substring(14) + "-3-" + s[4].Substring(1) + ".jpg";
                string fileNewDestination = fileNewFolder + fileName;

                FileInfo file = new FileInfo(fileToMove);
                if (file.Exists)
                {
                    //file.CopyTo(fileNewDestination);
                    file.MoveTo(fileNewDestination);
                    File.Delete(fileNameOld);
                    LogHelper.WriteLog(typeof(Service1), fileNewDestination + "转换成功");

                }
            }
            catch (System.Exception ex)
            {
                LogHelper.WriteLog(typeof(Service1), ex);

            }*/
        }

        protected override void OnStop()
        {
            LogHelper.WriteLog(typeof(Service1), "杰瑞转换电警命名服务关闭");

        }
        /// <summary>
        /// 初始化
        /// </summary>
        private void Init()
        {
            try
            {
                gOldFolder = ConfigurationManager.AppSettings["OldFolder"];

            }
            catch (System.Exception ex)
            {
                LogHelper.WriteLog(typeof(Service1), ex.Message);

            }
            if (!Directory.Exists(gOldFolder)) Directory.CreateDirectory(gOldFolder);
            fileSystemWatcher1.Path = gOldFolder;
        }
        private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            //ThreadPool.SetMaxThreads(50, 50);
            //ThreadPool.QueueUserWorkItem(convertDJ, e.FullPath);           

        }

        private void convertDJ(object fileNameObj)
        {
            try
            {
	            string fileNameStr = fileNameObj.ToString();
	            string fileNameOld = System.IO.Path.GetFileNameWithoutExtension(fileNameStr);//文件名  
	            string[] s = fileNameOld.Split(new char[] { '_' });
	
	            string fileToMove = fileNameStr;
	            string roadCode = s[0];
	            string fileNewFolder = ConfigurationManager.AppSettings[roadCode];
	            if (!Directory.Exists(fileNewFolder)) Directory.CreateDirectory(fileNewFolder);
	
	            string fileName = s[0] + s[1] + s[2].Substring(1) + "-" + s[3].Substring(0, 14) + "-" + s[3].Substring(14) + "-3-" + s[4].Substring(1) + ".jpg";
	            string fileNewDestination = fileNewFolder + fileName;
	
	            FileInfo file = new FileInfo(fileToMove);
	            if (file.Exists)
	            {
                    //file.CopyTo(fileNewDestination);
                    file.MoveTo(fileNewDestination);
	                File.Delete(fileNameOld);
	                LogHelper.WriteLog(typeof(Service1), fileNewDestination + "转换成功");
	
	            }
            }
            catch (System.Exception ex)
            {
                LogHelper.WriteLog(typeof(Service1), ex);

            }


        }

    }
}
